/*
 * @Author: xuxu12138 1718324422@qq.com
 * @Date: 2023-09-27 10:55:00
 * @LastEditors: xuxu12138 1718324422@qq.com
 * @LastEditTime: 2024-07-03 13:45:32
 * @FilePath: /things-admin/src/view/member/bloc/GivePointBloc.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Page } from '@/base/Page';
import {fetechPointList, getMember, queryBalance, savePoint} from '@/base/api/MemberApi';
import { Member } from '@/base/model/Member';
import { MemberSpecific } from '@/base/model/MemberSpecific';
import { isEmpty } from '@/base/util/Strings';
import {PointList} from "@/base/model/PointList";
import {formatDate} from "@/base/util/Dates";

class Form {
  phone = '';
  createBegin = '';
  createEnd = '';
  date = [];
  companyId = 0;
}

export class GivePointBloc {
  form = new Form();

  paged: Page<PointList> = new Page<PointList>([], 0, 1, 15, 0, []);
  loading = false;
  givesymbol = false;
  agentBalance = 0;
  givePoint = 1;

  start(id: number) {
    console.log(id,'dsaka')
    return getMember(id)
      .then((res) => {
        this.form.phone = res.data.phone;
        this.form.companyId = res.data.companyId;
        this.load(this.paged.pageIndex, this.paged.pageSize);
      });
  }
  load(pageIndex: number, pageSize: number) {
    if (!isEmpty(this.form.date) && !isEmpty(String(this.form.date[0]))) {
      // @ts-ignore
      this.form.createBegin = formatDate(new Date(this.form.date[0].getTime()), 'yyyy-MM-dd') || '';
      // @ts-ignore
      this.form.createEnd = formatDate(new Date(this.form.date[1].getTime() + 24 * 60 * 60 * 1000), 'yyyy-MM-dd') || '';
    } else {
      this.form.createBegin = '';
      this.form.createEnd = '';
    }
    this.loading = true;
    return fetechPointList({
      pageIndex,
      pageSize,
      companyId: this.form.companyId,
      createBegin: this.form.createBegin,
      createEnd: this.form.createEnd,
      phone: this.form.phone,
      description: 21
    }).then((res) => {
      this.paged = res.data;
    }).finally(() => {
      this.loading = false;
    });
  }

  reset() {
    this.form.date = [];
    this.form.createBegin = '';
    this.form.createEnd = '';
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.load(this.paged.pageIndex, this.paged.pageSize);
  }

  // 查询积分余额
  queryBalance() {
    return queryBalance().then((res) => {
      if (res.data !== null) {
        // @ts-ignore
        this.agentBalance = res.data;
      }
    });
  }
  savePoint() {
    return savePoint(
      {
        num: this.givePoint,
        phone: this.form.phone
      }
    ).then(
      (res) => {
          this.givePoint = 1;
          this.givesymbol = false;
          this.reset();
      }
    )
  }
}
